﻿using System;
using System.Data;
using StockTrader.QuoteCenter.Streamer.BLL;
using QuoteCenter.DataContract;
using System.Timers;

namespace StockTrader.QuoteCenter.Streamer.DAL
{
    public class DBAdapter
    {
        public delegate void QuotesChangedDeleaget(QuotesEventArgs args);
        public static event QuotesChangedDeleaget OnQuotesChanged;
        public static bool Initialized { get; private set; }
        private static Timer oStocksTimer;
        public static QuotesUI quote;
        static DBAdapter()
        {
            Initialized = true;
            //Start a timer that simulate a change to the stocks
            oStocksTimer = new Timer();
            oStocksTimer.Interval = 2000;
            oStocksTimer.Elapsed += new ElapsedEventHandler(StocksTimer_Elapsed);
            oStocksTimer.Enabled = true;
            oStocksTimer.Start();
        }

        private static void StocksTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //Stop the timer and update the stocks
            oStocksTimer.Stop();

            oStocksTimer.Start();
            if (OnQuotesChanged != null)
            {
                OnQuotesChanged(new QuotesEventArgs(quote));
            }
        }
    }
}
